package com.awenhui.demo.service;


import com.awenhui.demo.dao.MetaDataMapper;
import com.awenhui.demo.model.rbac.UserModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@CacheConfig(cacheNames = "fm_cache_redis_demo")
@Service
public class DemoService {

    @Autowired
    public MetaDataMapper demoMapper;

    /**
     * 获取时间
     *
     * @return
     */
    @Cacheable
    public String getTime(String id) {
        System.out.println("首次执行设置cache");
        return id + "_" + System.currentTimeMillis();
    }

    // @CachePut(key ="#model.userId" )
    public String putTime(UserModel model) {
        System.out.println("设置cache");
        return model.getUserId() + "_" + System.currentTimeMillis();
    }

    @CacheEvict(key = "#model.userId", allEntries = true)
    public String ClearCache(UserModel model) {
        return System.currentTimeMillis() + "";
    }

    @Cacheable
    public List<String> getList() {
        List<String> list = new ArrayList<>();
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
        return list;
    }

    /**
     * 获取所有表名
     *
     * @param schema 数据库(不填为当前数据库)
     * @return
     */
    public List<String> getTables(String schema) {


        List<HashMap<String, String>> tables = demoMapper.getTables(schema);
        List<String> tableNames = tables.stream().map(r -> r.get("TABLE_NAME")).collect(Collectors.toList());

        return tableNames;
    }




}